/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Tool;

import java.io.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author wcss
 */
public class FileCodeMaker 
{
    /**
     * 获取文件信息
     * @param f
     * @return 
     */
    public static byte[] getFileInfo(File f) throws Exception
    {
        if (f.exists())
        {
            byte[] cnt = new byte[36];
            RandomAccessFile fs = null;
            try {
                fs = new RandomAccessFile(f,"rw");                
                long size = f.length();
                long piece = size / 6;
                
                for(int k = 0;k < 6;k++)
                {
                    fs.seek(k * piece);
                    fs.read(cnt, k * 6, 6);
                }                
                
            } catch (Exception ex) {
                Logger.getLogger(FileCodeMaker.class.getName()).log(Level.SEVERE, null, ex);
                throw ex;
            }finally
            {
                fs.close();
            }
            
            return cnt;
        }else
        {
            return null;
        }
    }
    
    /**
     * 获取文件特征码
     * @param ff
     * @return 
     */
    public static String makeFileCode(File ff) throws Exception
    {
        byte[] cnt = FileCodeMaker.getFileInfo(ff);
        StringBuilder sb = new StringBuilder();
        for(byte bye : cnt)
        {
           byte val = bye;
           if (val < 0)
           {
               val =(byte)-val;
           }else if (val == 0)
           {
               val = 80;
           }
           sb.append(Integer.toHexString((int)val).toUpperCase());
        }
        return sb.toString();        
    }

        /**
     * 获取数据信息
     * @param f
     * @return 
     */
    public static byte[] getBytesInfo(byte[] source) throws Exception
    {
        if (source != null && source.length > 0)
        {
            byte[] cnt = new byte[36];
            try {                               
                long size = source.length;
                long piece = size / 6;
                
                for(int k = 0;k < 6;k++)
                {
                    System.arraycopy(source, (int)(k * piece), cnt, k * 6, 6);
                }                
                
            } catch (Exception ex) {
                Logger.getLogger(FileCodeMaker.class.getName()).log(Level.SEVERE, null, ex);
                throw ex;
            }finally
            {
            }
            
            return cnt;
        }else
        {
            return null;
        }
    }
    
    /**
     * 获取数据特征码
     * @param ff
     * @return 
     */
    public static String makeBytesCode(byte[] source) throws Exception
    {
        byte[] cnt = FileCodeMaker.getBytesInfo(source);
        StringBuilder sb = new StringBuilder();
        for(byte bye : cnt)
        {
           byte val = bye;
           if (val < 0)
           {
               val =(byte)-val;
           }else if (val == 0)
           {
               val = 80;
           }
           sb.append(Integer.toHexString((int)val).toUpperCase());
        }
        return sb.toString();        
    }

    public static void main(String[] args)
    {
        try {
            System.out.println(makeFileCode(new File("/home/wcss/视频/QvodMovies/5F7AF402A506712B293A9BC0C813613B29861C87+501946933.rmvb")));
            
            ByteBuffer bb = ByteBuffer.allocate((int)new File("/home/wcss/视频/QvodMovies/5F7AF402A506712B293A9BC0C813613B29861C87+501946933.rmvb").length());
            new FileInputStream("/home/wcss/视频/QvodMovies/5F7AF402A506712B293A9BC0C813613B29861C87+501946933.rmvb").getChannel().read(bb);
            System.out.println(makeBytesCode(bb.array()));
        } catch (Exception ex) {
            Logger.getLogger(FileCodeMaker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
